package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserProject
//@description: 创建UserProject记录
//@param: userProject model.UserProject
//@return: err error

func CreateUserProject(userProject model.UserProject) (err error) {
	err = global.GVA_DB.Create(&userProject).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserProject
//@description: 删除UserProject记录
//@param: userProject model.UserProject
//@return: err error

func DeleteUserProject(userProject model.UserProject) (err error) {
	err = global.GVA_DB.Delete(&userProject).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserProjectByIds
//@description: 批量删除UserProject记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserProjectByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserProject{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserProject
//@description: 更新UserProject记录
//@param: userProject *model.UserProject
//@return: err error

func UpdateUserProject(userProject model.UserProject) (err error) {
	err = global.GVA_DB.Save(&userProject).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserProject
//@description: 根据id获取UserProject记录
//@param: id uint
//@return: err error, userProject model.UserProject

func GetUserProject(id uint) (err error, userProject model.UserProject) {
	err = global.GVA_DB.Where("id = ?", id).Preload("Users").Preload(`UserProjectRefund`).Preload("Users.UserLevel").Preload("SysUser").First(&userProject).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserProjectInfoList
//@description: 分页获取UserProject记录
//@param: info request.UserProjectSearch
//@return: err error, list interface{}, total int64

func GetUserProjectInfoList(info request.UserProjectSearch,types int) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserProject{})
    var userProjects []model.UserProject
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.ProName != ``{
		db = db.Where("`pro_name` LIKE ?","%"+ info.ProName+"%")
	}
	if info.ConUnit != ``{
		db = db.Where("`con_unit` LIKE ?","%"+ info.ProName+"%")
	}
    if info.NowStatus != 0 {
        db = db.Where("`now_status` = ?",info.NowStatus)
    }
    if info.UserId != 0 {
        db = db.Where("`user_id` = ?",info.UserId)
    }

	if types == 1 {
		db = db.Where(`now_status in (3,7)` )
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload("Users").Preload("Users.UserLevel").Preload(`UserProjectRefund`).Preload("SysUser").Order(`id desc`).Find(&userProjects).Error
	return err, userProjects, total
}

func UserProjectTotal()(allCount int64,applyCount int64,overCount int64){
	db := global.GVA_DB.Model(&model.UserProject{})

	db.Count(&allCount)
	db = global.GVA_DB.Model(&model.UserProject{})
	db.Where("now_status",10).Count(&applyCount)
	db = global.GVA_DB.Model(&model.UserProject{})
	db.Where("now_status",2).Count(&overCount)
	return
}
//当前状态， 0待审核，1待授信，2授信中，3还款中，4已延迟，5冻结中，6完成'
func UserProjectTotalMoney()(sum float64,err error)  {
	db := global.GVA_DB.Model(&model.UserProject{})
	err = db.Pluck(`COALESCE(SUM(apply_price), 0) as amount1`, &sum).Error
	return
}